package javalee.util;

import java.io.*;
import java.awt.*;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.FontSelector;
import java.util.Iterator;
import javalee.maps.objetoImpressao;
import javalee.maps.produtosNota;

public class GeraNotaPDF
         {                
    
    public static void GeraNota(objetoImpressao obj)throws Exception
    {
        java.text.NumberFormat nf = java.text.NumberFormat.getNumberInstance();  
        nf.setMinimumFractionDigits(2);  
        nf.setMaximumFractionDigits(2);        
        
         Document document=new Document(PageSize.A4,50,50,50,50);
               PdfWriter.getInstance(document,new FileOutputStream("notaFiscal.pdf"));
               document.open();
               
               document.add(new Paragraph("          NOTA FISCAL DE VENDA",new
 Font(Font.FontFamily.HELVETICA, 18)));
               document.add(new Paragraph(" "));
               

               
               
               PdfPTable table=new PdfPTable(3);
               PdfPCell cell = new PdfPCell();
               cell.setColspan (3);
               cell.setHorizontalAlignment (Element.ALIGN_CENTER);
               cell.setPadding (10.0f);
               
               PdfPTable table_dados=new PdfPTable(3);
               table_dados.setWidths(new int[]{7,4,4});
               cell.setPadding (10.0f);
               table_dados.addCell (new Paragraph("Insc estadual do substituto tributario:\n0000000000 ",new
 Font(Font.FontFamily.HELVETICA, 8)));
              
               table_dados.addCell (new Paragraph("Inscricao estadual:\n000000000",new
 Font(Font.FontFamily.HELVETICA, 8)));
               table_dados.addCell (new Paragraph("CNPJ:\n0000000000",new
 Font(Font.FontFamily.HELVETICA, 8)));
               document.add(table);
               document.add(table_dados);
               
               document.add(new Paragraph("              Destinatario Remetente",new
 Font(Font.FontFamily.HELVETICA, 12)));
               document.add(new Paragraph(" ",new
 Font(Font.FontFamily.HELVETICA, 12)));
  
               PdfPTable table_nome=new PdfPTable(3);
               PdfPCell cell_nome = new PdfPCell();
               cell.setColspan (3);
               cell.setHorizontalAlignment (Element.ALIGN_CENTER);
               cell.setPadding (10.0f);
               
               PdfPTable table_nomeR=new PdfPTable(3);
               cell.setPadding (10.0f);
               table_nomeR.addCell (new Paragraph("Nome/Razao Social:\n "+obj.nome,new
 Font(Font.FontFamily.HELVETICA, 8)));
               table_nomeR.addCell (new Paragraph("CNPJ/CPF:\n",new
 Font(Font.FontFamily.HELVETICA, 8)));
               table_nomeR.addCell (new Paragraph("Municipio:\n",new
 Font(Font.FontFamily.HELVETICA, 8)));
               
               table_nomeR.addCell (new Paragraph("Endereço:\n",new
 Font(Font.FontFamily.HELVETICA, 8)));
               table_nomeR.addCell (new Paragraph("Bairro:\n",new
 Font(Font.FontFamily.HELVETICA, 8)));
               table_nomeR.addCell (new Paragraph("CEP:\n",new
 Font(Font.FontFamily.HELVETICA, 8)));
               
               table_nomeR.addCell (new Paragraph("Telefone:\n",new
 Font(Font.FontFamily.HELVETICA, 8)));
               table_nomeR.addCell (new Paragraph("UF:\n",new
 Font(Font.FontFamily.HELVETICA, 8)));
               table_nomeR.addCell (new Paragraph("Inscricao Estadual:\n",new
 Font(Font.FontFamily.HELVETICA, 8)));
               
               table_nomeR.addCell (new Paragraph("Data da Emissão:\n"+obj.emissao,new
 Font(Font.FontFamily.HELVETICA, 8)));
               table_nomeR.addCell (new Paragraph("Data Saida/Entrada:\n"+obj.saida,new
 Font(Font.FontFamily.HELVETICA, 8)));
               table_nomeR.addCell (new Paragraph("Hora de Saida:\n"+obj.hora,new
 Font(Font.FontFamily.HELVETICA, 8)));
               
               document.add(table_nome);
               document.add(table_nomeR);
               
               document.add(new Paragraph("              Dados do Produto",new
 Font(Font.FontFamily.HELVETICA, 12)));
               document.add(new Paragraph(" "));
               
               PdfPTable tableProd=new PdfPTable(8);
               PdfPCell cellProd = new PdfPCell();
               cell.setColspan (8);
               cell.setHorizontalAlignment (Element.ALIGN_CENTER);
               cell.setPadding (10.0f);
               
               PdfPTable tableProd2;
                       
               tableProd2 = new PdfPTable(8);
               tableProd2.setWidths(new int[]{2,7,3,2,2,2,2,2});
               cell.setPadding (10.0f);
               tableProd2.addCell (new Paragraph("Código Produto\n",new
 Font(Font.FontFamily.HELVETICA, 7)));
               tableProd2.addCell (new Paragraph("Descrição",new
 Font(Font.FontFamily.HELVETICA, 7)));
               tableProd2.addCell (new Paragraph("Quantidade",new
 Font(Font.FontFamily.HELVETICA, 7)));
               tableProd2.addCell (new Paragraph("Valor Unitário",new
 Font(Font.FontFamily.HELVETICA, 7)));
               tableProd2.addCell (new Paragraph("Valor Total",new
 Font(Font.FontFamily.HELVETICA, 7)));
               tableProd2.addCell (new Paragraph("ICMS",new
 Font(Font.FontFamily.HELVETICA, 7)));
               tableProd2.addCell (new Paragraph("IPI",new
 Font(Font.FontFamily.HELVETICA, 7)));
               tableProd2.addCell (new Paragraph("Valor do IPI",new
 Font(Font.FontFamily.HELVETICA, 7)));
               
               document.add(tableProd);
               document.add(tableProd2);
               
               for(Iterator<produtosNota> it = obj.prods.iterator(); it.hasNext();){
                    tableProd2 = new PdfPTable(8);
                    tableProd2.setWidths(new int[]{2,7,3,2,2,2,2,2});
         
                    produtosNota p = (produtosNota)it.next();
                    
               cell.setPadding (10.0f);
               tableProd2.addCell (new Paragraph(p.codProduto,new
 Font(Font.FontFamily.HELVETICA, 8)));
               tableProd2.addCell (new Paragraph(p.descricao,new
 Font(Font.FontFamily.HELVETICA, 8)));
               tableProd2.addCell (new Paragraph(p.qtde.toString() ,new
 Font(Font.FontFamily.HELVETICA, 8)));
               tableProd2.addCell (new Paragraph(nf.format(p.valor).toString() ,new
 Font(Font.FontFamily.HELVETICA, 8)));
               tableProd2.addCell (new Paragraph(nf.format(p.valorTotal).toString(),new
 Font(Font.FontFamily.HELVETICA, 8)));
               tableProd2.addCell (new Paragraph(nf.format(p.ICMS).toString(),new
 Font(Font.FontFamily.HELVETICA, 8)));
               tableProd2.addCell (new Paragraph(nf.format(p.IPI).toString(),new
 Font(Font.FontFamily.HELVETICA, 8)));
               tableProd2.addCell (new Paragraph(nf.format(p.valorIPI).toString(),new
 Font(Font.FontFamily.HELVETICA, 8)));                    
               document.add(tableProd2);
               }
              
               
               
               
               document.add(new Paragraph("              Calculo de Impostos",new
 Font(Font.FontFamily.HELVETICA, 12)));
               document.add(new Paragraph(" "));
              
               PdfPTable tableImp=new PdfPTable(5);
               PdfPCell cellImp = new PdfPCell();
               cell.setColspan (5);
               cell.setHorizontalAlignment (Element.ALIGN_CENTER);
               cell.setPadding (10.0f);
               
               PdfPTable tableImp2=new PdfPTable(5);
               tableImp2.setWidths(new int[]{5,3,3,3,3});
               cell.setPadding (10.0f);
               tableImp2.addCell (new Paragraph("Base de Calculo do ICMS\n"+nf.format(obj.baseICMS),new
 Font(Font.FontFamily.HELVETICA, 7)));
               tableImp2.addCell (new Paragraph("Valor do ICMS\n"+nf.format(obj.valorICMS),new
 Font(Font.FontFamily.HELVETICA, 7)));
               tableImp2.addCell (new Paragraph("Base de Calculo do ICMS substituição",new
 Font(Font.FontFamily.HELVETICA, 7)));
               tableImp2.addCell (new Paragraph("Valor do ICMS substituição",new
 Font(Font.FontFamily.HELVETICA, 7)));
               tableImp2.addCell (new Paragraph("Valor Total Produtos\n"+nf.format(obj.valorTotal),new
 Font(Font.FontFamily.HELVETICA, 7)));
               
               tableImp2.addCell (new Paragraph("Valor do frete",new
 Font(Font.FontFamily.HELVETICA, 7)));
               tableImp2.addCell (new Paragraph("Valor do seguro",new
 Font(Font.FontFamily.HELVETICA, 7)));
               tableImp2.addCell (new Paragraph("Valor total do IPI\n"+nf.format(obj.valorIPI),new
 Font(Font.FontFamily.HELVETICA, 7)));
               tableImp2.addCell (" ");
               tableImp2.addCell (new Paragraph("Valor Total da nota\n"+obj.valorTotal,new
 Font(Font.FontFamily.HELVETICA, 7)));
               
               document.add(tableImp);
               document.add(tableImp2);
               
               document.add(new Paragraph("              Numero da Nota Fiscal:",new
 Font(Font.FontFamily.HELVETICA, 12)));
               document.add(new Paragraph(" "));
               
               PdfPTable tableNum=new PdfPTable(1);
               tableNum.addCell(obj.numNota);
               document.add(tableNum);
               
               document.close();
               
               Desktop.getDesktop().open (new File ("notaFiscal.pdf")); 
    }
    }
            

